#include <stdio.h>
#include "ofc/DDbm.h"

int main(int argc, char *argv[])
{
#ifdef HAVE_DDBM
  DDbm  *dbm1 = [DDbm alloc];
  DDbm  *dbm2 = [DDbm new  ];
  DData *dat;
  DText *str;

  [dbm1 init :"file.dbm" :"c"];        // Init and open with filename, writer

  if ([dbm1 isOpen])                   // Check if the file is open
  {
    if ([dbm1 insert :"key1" :4 :"data1" :5])
      printf("Data: key1,data1 succesfull inserted.\n");
    else
      printf("Error inserting: key1,data1:%d.\n", [dbm1 error]);

    if ([dbm1 insert :"key2" :4 :"data2" :5])
      printf("Data: key2,data2 succesfull inserted.\n");
    else
      printf("Error inserting: key2,data2:%d.\n", [dbm1 error]);

    [dbm1 close];
  }
  else
    printf("Error opening file.dbm:%d.\n", [dbm1 error]);


  if ([dbm2 open :"file.dbm" :"r"])    // Open the file for reading
  {
    dat = [dbm2 get :"key2" :4];       // Get the data for key2

    if (dat != nil)
    {
      str = [dat toText];
      printf("Data for key2:%s.\n", [str cstring]);
      [str free];
      [dat free];
    }
    else
      printf("Key2 is not present in the file.\n");

    if ([dbm2 get :"key3" :4])         // Check for key3
      printf("Key3 is present in the file.\n");
    else
      printf("Key3 is not present in the file.\n");

    [dbm2 close];
  }
  else
    printf("Error opening file.dbm:%d.\n", [dbm2 error]);

  [dbm1 free];                         // Cleanup
  [dbm2 free];
#else
  printf("DDbm is not present in the library.\n");
#endif
  return 0;
}

